<?php
/**
*  +----------------------------------------------------------------------------------------------+
*   | Explain:价格
*  +----------------------------------------------------------------------------------------------+
*   | Author: ONLY <491518132@qq.com>
*	+----------------------------------------------------------------------------------------------+
*   | Creater Time : 2013-10-31 	
*  +----------------------------------------------------------------------------------------------+
*   | Link :		http://www.phpyrb.com	     
*  +----------------------------------------------------------------------------------------------+
**/
class PriceAction extends BaseAction {
	protected $product, $newpro;
	function _initialize() {
		parent::_initialize ();
		import ( 'Com.Product' );
		import ( 'Com.Newpro' );
		$this->product = new Product ();
		$this->newpro = new Newpro ();
	}
	function index() {
		$mname = $_REQUEST ['mname'] ? $_REQUEST ['mname'] : 'sj';
		
		$count = $this->newpro->modetopro ( $mname, '', true );
		
		$page = new Page ( $count, $this->listrows );
		$offset = $page->firstRow;
		$length = $page->listRows;
		$limit = "$offset , $length";
		
		$proids = $this->newpro->modetopro ( $mname, $limit );
		$proinfo = $this->newpro->protobaseinfo ( $proids );
		$price = $this->newpro->b2btoprice ( $proids );
		// dump($price);
		
		$this->assign ( "totalCount", $count );
		$this->assign ( "sfield_name", $sfield_name );
		$this->assign ( 'list', $proinfo );
		$this->assign ( 'price', $price );
		$this->display ();
	}
	
	function edit(){
		$this->display();
	}
	
	function add(){
		$price = array();
		$filePath = '';
		if ($_FILES ['price']['name']) {
			echo 123;
			$filePath = $this->_upload ();
			$_SESSION ['file'] = $filePath;
		} else {
			$filePath = $_SESSION ['file'];
		}
		$price = $this->read_xls($filePath);
		
		dump($price);
	}
	
	
	
	function read_xls($file='',$start_row=2,$Sheet=0) {
		//读取并分析exce
		if (! file_exists ( $file )) {
			return '-'.__LINE__;
		}
		import("@.Com.PHPExcel",'','.php');
		//import("@.Com.PHPExcel.IOFactory",'','.php');
		import("@.Com.PHPExcel.Reader.Excel5", '','.php');
		import("@.Com.PHPExcel.Reader.Excel2007",'','.php');
		$PHPExcel = new PHPExcel ();
		$PHPReader = new PHPExcel_Reader_Excel2007 ();
		if (! $PHPReader->canRead ( $file )) {
			$PHPReader = new PHPExcel_Reader_Excel5 ();
			if (! $PHPReader->canRead ( $file )) {
				return '-'.__LINE__;
			}
		}
		$objPHPExcel = $PHPReader->load ( $file );
		$sheet = $objPHPExcel->getSheet ($Sheet); // 读取第一個工作表(编号从 0 开始)
		//$sheet = $objPHPExcel->getSheetByName ( $sheet ); // 读取第一個工作表(编号从 0 开始)
		$highestRow = $sheet->getHighestRow (); // 取得总行数
		$highestColumn = $sheet->getHighestColumn (); // 取得总列数
		$arr = array (
				1 => 'A',2 => 'B',3 => 'C',4 => 'D',
				5 => 'E',6 => 'F',7 => 'G',8 => 'H',9 => 'I',10 => 'J',11 => 'K',12 => 'L',
				13 => 'M',14 => 'N',15 => 'O',16 => 'P',17 => 'Q',18 => 'R',19 => 'S',20 => 'T',
				21 => 'U',22 => 'V',23 => 'W',24 => 'X',25 => 'Y',26 => 'Z');
		$column = array_search ( $highestColumn, $arr );
		//读取表中的数据
		$data = array ();
		for($x = $start_row; $x <= $highestRow + 1; $x ++) {
			for($i=0;$i<=15;$i++){
				$row = '';
				$row = $sheet->getCellByColumnAndRow ( $i, $x )->getValue ();
				$data[$x][$i]=trim($row);//不使用trim有可能获取到的是一个对象
			}
		}
		return $data;
	}//end read_xls
	
	/**
	 * 处理文件上传
	 * @return string
	 */
	function _upload() {
		// $upfile=F('upfile');
		import ( "Com.UploadFile" );
		$upload = new UploadFile ();
		// 设置上传文件大小
		$upload->maxSize = 3292200;
		// 设置上传文件类型
		$upload->allowExts = explode ( ',', 'xls,xlsx,jpg' );
		// 设置附件上传目录
		$path = date ( 'Y-m' ) . '/';
		$upload->savePath = './Uploads/' . $path;
		if(! mkdir ( $upload->savePath)){
			echo '创建文件夹失败！';
		}
		$path = 'Uploads/' . $path;
		// 设置需要生成缩略图，仅对图像文件有效
		// 设置上传文件规则
		$upload->saveRule = 'time';
		$upload->uploadReplace = true;
		if (! $upload->upload ()) {
			// 捕获上传异常
// 			$this->error ( $upload->getErrorMsg () );
		} else {
			// 取得成功上传的文件信息
			$uploadList = $upload->getUploadFileInfo ();
			$_POST ['picurl'] = $path . $uploadList [0] ['savename'];
		}
		return $_POST ['picurl'];
	}
}